<template>
  <Container>
    <template v-slot:searchForm>
      <div class="searchRow">
        <el-form class="elForm" ref="form" :model="searchFormData" label-width="80px">
          <!-- <el-form-item label="功能名称">
            <el-input v-model="searchFormData.funcName" clearable placeholder="请输入"></el-input>
          </el-form-item> -->
          <!-- <el-form-item label="数据类型">
            <el-input v-model="searchFormData.dataType" clearable placeholder="请输入"></el-input>
          </el-form-item> -->
        </el-form>
        <div class="btnList">
          <!-- <el-button type="danger" icon="el-icon-delete" @click="handleClickDel(selectionList)"
          :disabled="selectionList.length == 0" plain>删
          除</el-button> -->
          <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleClickAddEdit('add')"
            plain>添加</el-button>
          <el-button type="primary" icon="el-icon-search" @click="queryTableList" plain>查询</el-button>
        </div>
      </div>
    </template>
    <template v-slot:table>
      <el-table v-loading="loading" :data="tableData" border height="100%" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="55" label="序号" />
        <el-table-column label="app类型" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.appType ? scope.row.appType.toLowerCase() : '--' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新内容" prop="updateContent" width="150">
        </el-table-column>
        <el-table-column label="路径" prop="downloadUrl">
        </el-table-column>
        <el-table-column label="版本" prop="version" width="150">
        </el-table-column>
        <el-table-column label="是否强制更新" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.updates == true ? '是' : '否' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" width="200">
          <template slot-scope="scope">
            <el-switch style="display: block" v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
              active-text="启用" inactive-text="停用" @change="handelChangeEnable(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <!-- <el-button type="text" @click="handleClickAddEdit('edit', scope.row)">编辑</el-button> -->
            <el-button type="text" @click="handleClickDel(scope.row.versionManageId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <AddEdit :title="'版本'" @closeDialog="closeDialog" :addEditFlag="addEditFlag" :dialogVisible="dialogVisible"
        :data="formData">
      </AddEdit>
    </template>
    <template v-slot:pagination>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="page" :page-sizes="[10, 100, 200, 300, 400, 500, 600]" :page-size="size"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </template>
  </Container>
</template>

<script>
import Container from "@/components/Container"
import AddEdit from './components/addEdit';
import versionManage from "@/api/versionManage"
export default {
  components: { Container, AddEdit },
  data() {
    return {
      searchFormData: {},
      tableData: [],
      selectionList: [],
      page: 1,
      size: 100,
      total: 10,
      addEditFlag: 'add',
      dialogVisible: false,
      formData: {},
      loading: false
    }
  },
  watch: {},
  mounted() {
    this.queryTableList();
  },
  methods: {
    queryTableList() {
      let params = {
        page: this.page,
        size: this.size,
        condition: {
          ...this.searchFormData
        }
      }
      this.loading = true;
      versionManage.queryPageList(params).then(res => {
        this.loading = false;
        let { data } = res
        this.total = data.total;
        this.tableData = data.list;
      }).catch(err => {
        this.loading = false;
        console.log(err)
      })
    },
    closeDialog() {
      this.dialogVisible = false;
      this.formData = {};
      this.queryTableList();
    },
    async handelChangeEnable(row) {
      let res = await versionManage.versionManageEnable(row.versionManageId);
      this.queryTableList();
      if (res.code == 1) {
        this.$message.success('操作成功')
      } else {
        this.$message.warning(res.msg || '操作失败')
      };
    },
    handleClickDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        versionManage.del(id).then(res => {
          if (res.code == 1) {
            this.$message.success('删除成功')
          } else {
            this.$message.warning(res.msg || '删除失败')
          }
          this.queryTableList();
        }).catch(err => {
          this.$message.warning('删除失败')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleClickAddEdit(flag = 'add', row = {}) {
      if (flag == 'add') {
        this.$confirm('您确定添加新版本? 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.formData = JSON.parse(JSON.stringify(row));
          this.addEditFlag = flag
          this.dialogVisible = true;
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      } else {
        this.formData = JSON.parse(JSON.stringify(row));
        this.addEditFlag = flag
        this.dialogVisible = true;
      }
    },
    handleSelectionChange(list = []) {
      this.selectionList = list
    },
    handleSizeChange(size) {
      this.size = size;
      this.queryTableList();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryTableList();
    },
  }
}
</script>

<style lang="scss" scoped>
.row {
  padding-bottom: 15px;
}
</style>
